home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / LFolderWatcher (PP) / LFolderWatcher.sit / LFolderWatcher / LFolderWatcher.cp < prev    next >
Text File  |  1995-08-27  |  5KB  |  184 lines

  1. // ===========================================================================
  2. //    LFolderWatcher.cp                ⌐ 1995, âric Forget. All rights reserved.
  3. // ===========================================================================
  4. //    
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    LFolderWatcher, as its name tell, watch a folder for any modification in
  13. //    it. LFolderWatcher is a subclass of LSortedList.
  14. //
  15. // ---------------------------------------------------------------------------
  16. //
  17. //    Instruction Notes:
  18. //    -----------------
  19. //
  20. //    1) Create a LFolderWatcher object: inFileCreator and inFileType are optional;
  21. //
  22. //    2) Do:    if(myWatcher->Update()) {
  23. //
  24. //                // The contains of the folder has changed!
  25. //
  26. //                LListIterator iterator(*myWatcher, iterate_FromStart);
  27. //                FSSpec    theSpec;
  28. //                while (iterator.Next(&theSpec)) {
  29. //                
  30. //                    // Do what you need with theSpec
  31. //                }
  32. //            }
  33. //
  34. // ---------------------------------------------------------------------------
  35. //
  36. //    Usage Notes:
  37. //    -----------
  38. //
  39. //    If you specify inFileCreator or InFileType with "fileType_Default"
  40. //    LFolderWatcher will interpret it as any type.
  41. //
  42. // ---------------------------------------------------------------------------
  43.  
  44.  
  45. #include    "LFolderWatcher.h"
  46. #include    "LFSSpecComparator.h"
  47.  
  48.  
  49. // ---------------------------------------------------------------------------
  50. //        Ñ Constants
  51. // ---------------------------------------------------------------------------
  52.  
  53. const    char    Mask_IsDirectory                    = 0x0010;
  54. const    char    kIsAlias                            = 0x8000;
  55.  
  56.  
  57. // ---------------------------------------------------------------------------
  58. //        Ñ LFolderWatcher
  59. // ---------------------------------------------------------------------------
  60.  
  61. LFolderWatcher::LFolderWatcher(
  62.     Int32    inDirID,
  63.     Int16    inVRefNum,
  64.     OSType    inFileCreator,
  65.     OSType    inFileType)
  66.         : LSortedList(sizeof(FSSpec))
  67. {
  68.     mDirID            = inDirID;
  69.     mVRefNum        = inVRefNum;
  70.     mFileType        = inFileType;
  71.     mFileCreator    = inFileCreator;
  72.     
  73.     mLastModificationDate = 0;
  74.     
  75.     
  76.     SetComparator(new LFSSpecComparator);
  77. }
  78.  
  79.  
  80. // ---------------------------------------------------------------------------
  81. //        Ñ ~LFolderWatcher
  82. // ---------------------------------------------------------------------------
  83.  
  84. LFolderWatcher::~LFolderWatcher()
  85. {
  86. }
  87.  
  88.  
  89. // ---------------------------------------------------------------------------
  90. //        Ñ Update
  91. // ---------------------------------------------------------------------------
  92.  
  93. Boolean
  94. LFolderWatcher::Update()
  95. {
  96.     OSErr            err;
  97.     CInfoPBRec        cBlock;
  98.     Str63            stringName;
  99.     Boolean            rval = false;
  100.     
  101.     
  102.     cBlock.dirInfo.ioCompletion        = nil;
  103.     cBlock.dirInfo.ioVRefNum        = mVRefNum;
  104.     cBlock.dirInfo.ioDrDirID        = mDirID;
  105.     cBlock.dirInfo.ioNamePtr        = stringName;
  106.     cBlock.dirInfo.ioFDirIndex        = -1;
  107.     
  108.     err = ::PBGetCatInfoSync(&cBlock);
  109.     
  110.     if((err == noErr) && (cBlock.dirInfo.ioDrMdDat != mLastModificationDate)) {
  111.         
  112.         // The contains of the folder has changed!
  113.         
  114.         Reset();
  115.         mLastModificationDate = cBlock.dirInfo.ioDrMdDat;
  116.         rval = true;
  117.     }
  118.     
  119.     return rval;
  120. }
  121.  
  122.  
  123. // ---------------------------------------------------------------------------
  124. //        Ñ Reset
  125. // ---------------------------------------------------------------------------
  126.  
  127. void
  128. LFolderWatcher::Reset()
  129. {
  130.     OSErr            err;
  131.     CInfoPBRec        cBlock;
  132.     Str255            fileName;
  133.     FSSpec            fileSpec;
  134.     FInfo            fndrInfo;
  135.     
  136.     
  137.     cBlock.hFileInfo.ioCompletion    = nil;
  138.     cBlock.hFileInfo.ioNamePtr        = fileName;
  139.     cBlock.hFileInfo.ioFDirIndex    = 1;
  140.     
  141.     RemoveItemsAt(GetCount(), 1);
  142.     
  143.     do {
  144.     
  145.         cBlock.hFileInfo.ioVRefNum        = mVRefNum;
  146.         cBlock.hFileInfo.ioDirID        = mDirID;
  147.         
  148.         err = ::PBGetCatInfoSync(&cBlock);
  149.         cBlock.hFileInfo.ioFDirIndex++;
  150.         
  151.         if(err == noErr) {
  152.             
  153.             if(!(cBlock.dirInfo.ioFlAttrib & Mask_IsDirectory)) {
  154.             
  155.                 if((err = ::FSMakeFSSpec(mVRefNum, mDirID, fileName, &fileSpec)) == noErr) {
  156.                     
  157.                     Boolean    targetIsFolder, wasAliased;
  158.                     
  159.                     
  160.                     fndrInfo = cBlock.hFileInfo.ioFlFndrInfo;
  161.                     
  162.                     if((err = ::ResolveAliasFile(&fileSpec, true, &targetIsFolder, &wasAliased)) != noErr) {
  163.                         
  164.                         if(wasAliased) {
  165.                         
  166.                             err = ::FSpGetFInfo(&fileSpec, &fndrInfo);
  167.                         }
  168.                     }
  169.                     
  170.                     if((err == noErr) &&
  171.                        (mFileCreator == fileType_Default || fndrInfo.fdCreator == mFileCreator) &&
  172.                        (mFileType == fileType_Default || fndrInfo.fdType == mFileType)) {
  173.                         
  174.                         AddItem(&fileSpec);
  175.                     }
  176.                 }
  177.             }
  178.             
  179.             err = noErr;
  180.         }
  181.     
  182.     } while(err == noErr);
  183. }
  184.